package com.guoheng.hazard.sys.service.impl.entpr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoheng.hazard.common.base.Result;
import com.guoheng.hazard.common.enums.ErrorCodeEnum;
import com.guoheng.hazard.common.enums.ReviewStatusEnum;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicExamineMapper;
import com.guoheng.hazard.sys.mapper.entpr.EntprBasicInfoMapper;
import com.guoheng.hazard.sys.model.dto.entpr.CommitExaminationDTO;
import com.guoheng.hazard.sys.model.dto.entpr.DealExaminationDTO;
import com.guoheng.hazard.sys.model.dto.entpr.EntprBasicExamineDTO;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicExamine;
import com.guoheng.hazard.sys.model.entity.entpr.EntprBasicInfo;
import com.guoheng.hazard.sys.service.entpr.EntprBasicExamineService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 类功能描述:　企业基本信息审核
 *
 * @author fbl
 * @date 2019-11-11 18:10
 */
@Service
public class EntprBasicExamineImpl implements EntprBasicExamineService {

    @Resource
    EntprBasicExamineMapper entprBasicExamineMapper;

    @Resource
    EntprBasicInfoMapper entprBasicInfoMapper;

    @Override
    public Result getExaminations(Long entprId,Integer pageNum, Integer pageSize, String entprName, Integer reviewStatus) {
        PageHelper.startPage(pageNum, pageSize);
        List<EntprBasicExamineDTO> examinations = entprBasicExamineMapper.getExaminations(entprId,entprName, reviewStatus);
        PageInfo<EntprBasicExamineDTO> entprBasicExaminePageInfo = new PageInfo<>(examinations);
        return Result.success(entprBasicExaminePageInfo);
    }

    @Override
    public Result commitExamination(CommitExaminationDTO commitExaminationDTO) {
        EntprBasicExamine entprBasicExamine = new EntprBasicExamine();
        BeanUtils.copyProperties(commitExaminationDTO, entprBasicExamine);
        entprBasicExamine.setApplicationTime(new Date());
        // 待审核
        entprBasicExamine.setReviewStatus(ReviewStatusEnum.REVIEW_STATUS_1.getCode());
        entprBasicExamineMapper.insertExamination(entprBasicExamine);
        return Result.success();
    }

    @Override
    public Result dealExamination(Long id, DealExaminationDTO dealExaminationDTO) {

        EntprBasicExamine entprBasicExamineById = entprBasicExamineMapper.getEntprBasicExamineById(id);
        if (entprBasicExamineById.getReviewStatus().equals(ReviewStatusEnum.REVIEW_STATUS_2.getCode())  || entprBasicExamineById.getReviewStatus().equals(ReviewStatusEnum.REVIEW_STATUS_3.getCode())) {
            return Result.failure(ErrorCodeEnum.SYS_ERR_EXAMINE);
        }
        EntprBasicExamine entprBasicExamine = new EntprBasicExamine();
        BeanUtils.copyProperties(dealExaminationDTO, entprBasicExamine);
        entprBasicExamine.setId(id);
        entprBasicExamine.setReviewTime(new Date());
        entprBasicExamineMapper.updateExaminationById(entprBasicExamine);
        return Result.success();
    }

    @Override
    public Result getExaminationDetail(Long id) {
        EntprBasicExamineDTO entprBasicExamineDTO = new EntprBasicExamineDTO();
        EntprBasicExamine entprBasicExamineById = entprBasicExamineMapper.getEntprBasicExamineById(id);
        BeanUtils.copyProperties(entprBasicExamineById, entprBasicExamineDTO);
        Long entprId = entprBasicExamineById.getEntprId();
        EntprBasicInfo entprBasicInfoById = entprBasicInfoMapper.getEntprBasicInfoById(entprId);
        entprBasicExamineDTO.setEntprName(entprBasicInfoById.getEntprName());
        return Result.success(entprBasicExamineDTO);
    }
}